﻿using System;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace Als.Utils.Net
{
    public class EmailHelper
    {
        private readonly EmailAccount _accountInfo = new EmailAccount();
        private const string Tab = "\t";

        public EmailHelper(EmailAccount accountInfo)
        {
            _accountInfo.Account = accountInfo.Account;
            _accountInfo.DisplayName = accountInfo.DisplayName;
            _accountInfo.EnableSsl = accountInfo.EnableSsl;
            _accountInfo.Host = accountInfo.Host;
            _accountInfo.Password = accountInfo.Password;
            _accountInfo.Port = accountInfo.Port;
        }

        /// <summary>
        /// 发送邮件 同步
        /// </summary>
        /// <param name="toemails">接收邮箱</param>
        /// <param name="subject">主题</param>
        /// <param name="body">内容</param>
        /// <param name="attachments">附件</param>
        /// <returns></returns>
        public bool Send(string toemails, string subject, string body, params Attachment[] attachments)
        {
            var message = new MailMessage();
            var displayNameEncoding = Encoding.UTF8;
            message.From = new MailAddress(_accountInfo.Account, _accountInfo.DisplayName, displayNameEncoding);
            message.To.Add(toemails);
            message.Subject = subject;
            message.Body = body;
            message.Priority = MailPriority.High;
            message.BodyEncoding = displayNameEncoding;
            if ((attachments != null) && (attachments.Length > 0))
            {
                foreach (var attachment in attachments)
                {
                    message.Attachments.Add(attachment);
                }
            }

            var client = new SmtpClient
            {
                Host = _accountInfo.Host,
                Port = _accountInfo.Port,
                Credentials = _accountInfo.IsUnnormal
                    ? new NetworkCredential("", this._accountInfo.Account + "\t" + this._accountInfo.Password)
                    : new NetworkCredential(this._accountInfo.Account, this._accountInfo.Password),
                EnableSsl = this._accountInfo.EnableSsl
            };
            try
            {
                client.Send(message);
            }
            catch
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 发送邮件 异步
        /// </summary>
        /// <param name="toemails">接收邮箱</param>
        /// <param name="subject">主题</param>
        /// <param name="body">内容</param>
        /// <param name="attachments">附件</param>
        public void SendAsync(string toemails, string subject, string body, params Attachment[] attachments)
        {
            var message = new MailMessage();
            var displayNameEncoding = Encoding.UTF8;
            message.From = new MailAddress(_accountInfo.Account, _accountInfo.DisplayName, displayNameEncoding);
            message.To.Add(toemails);
            message.Subject = subject;
            message.Body = body;
            message.Priority = MailPriority.High;
            message.BodyEncoding = displayNameEncoding;
            if ((attachments != null) && (attachments.Length > 0))
            {
                foreach (var attachment in attachments)
                {
                    message.Attachments.Add(attachment);
                }
            }

            var client = new SmtpClient
            {
                Host = _accountInfo.Host,
                Port = _accountInfo.Port,
                Credentials = _accountInfo.IsUnnormal
                    ? new NetworkCredential("", _accountInfo.Account + "\t" + _accountInfo.Password)
                    : new NetworkCredential(_accountInfo.Account, _accountInfo.Password),
                EnableSsl = _accountInfo.EnableSsl
            };
            client.SendAsync(message, null);
        }


    }

    #region 邮箱配置

    public class EmailAccount
    {
        public EmailAccount()
        {
            Port = 0x19;
        }

        /// <summary>
        /// 账户信息
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <param name="host"></param>
        public EmailAccount(string account, string password, string host)
        {
            Port = 0x19;
            Host = host;
            Password = password;
            Account = account;
        }

        /// <summary>
        /// 账户信息
        /// </summary>
        /// <param name="account"></param>
        /// <param name="displayName"></param>
        /// <param name="password"></param>
        /// <param name="host"></param>
        public EmailAccount(string account, string displayName, string password, string host)
        {
            Port = 0x19;
            Host = host;
            Password = password;
            Account = account;
            DisplayName = displayName;
        }

        /// <summary>
        /// 账户信息
        /// </summary>
        /// <param name="account"></param>
        /// <param name="displayName"></param>
        /// <param name="password"></param>
        /// <param name="host"></param>
        /// <param name="port"></param>
        /// <param name="enableSsl"></param>
        /// <param name="isUnnormal"></param>
        public EmailAccount(string account, string displayName, string password, string host, int port, bool enableSsl, bool isUnnormal)
        {
            Port = 0x19;
            Host = host;
            Password = password;
            Account = account;
            DisplayName = displayName;
            Port = port;
            EnableSsl = enableSsl;
            IsUnnormal = isUnnormal;
        }

        /// <summary>
        /// 账户名
        /// </summary>
        public string Account { get; set; }
        /// <summary>
        /// 显示名称
        /// </summary>
        public string DisplayName { get; set; }
        /// <summary>
        /// 是否开启SSL
        /// </summary>
        public bool EnableSsl { get; set; }
        /// <summary>
        /// 主机地址
        /// </summary>
        public string Host { get; set; }
        /// <summary>
        /// 是否异常
        /// </summary>
        public bool IsUnnormal { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }
        /// <summary>
        /// 端口
        /// </summary>
        public int Port { get; set; }

    }

    #endregion


}
